from tortoise.models import Model
from tortoise import fields
from pydantic import BaseModel, Field
from datetime import datetime
from enum import Enum
from typing import Optional  # 确保正确导入

# 讨论状态枚举（可选）
class DiscussionStatus(str, Enum):
    ACTIVE = "active"
    CLOSED = "closed"
    PINNED = "pinned"


# Tortoise-ORM 模型
class Discussion(Model):
    discussion_id = fields.IntField(pk=True)  # 讨论ID（主键）
    title = fields.CharField(max_length=100)  # 讨论标题
    content = fields.TextField()  # 讨论内容
    created_at = fields.DatetimeField(auto_now_add=True)  # 创建时间
    updated_at = fields.DatetimeField(auto_now=True)  # 最后更新时间
    status = fields.CharEnumField(DiscussionStatus, default=DiscussionStatus.ACTIVE)  # 讨论状态

    # 多对一关系：多个讨论属于一个用户
    author = fields.ForeignKeyField(
        "models.Student",
        related_name="discussions",
        on_delete=fields.SET_NULL,  # 用户删除后保留讨论
        null=True
    )

    # 关联的课程ID（可以为空，表示通用讨论）
    discussion_class_id = fields.IntField(null=True)  # 关联class_id

    class Meta:
        table = "discussions"
        indexes = [("discussion_class_id",)]  # 为课程ID添加索引


# ---------- Pydantic 模型 ----------

    class Config:

        from_attributes = True